package com.zlt.service.impl;

import com.zlt.dao.Impl.RecruiteMenuDaoImpl;
import com.zlt.dao.RecruiteMenuDao;
import com.zlt.entity.RecruiteMenu;
import com.zlt.entity.Role;
import com.zlt.service.RecruiteMenuService;
import com.zlt.util.ActiveUserUtil;
import com.zlt.vo.ActiveUser;
import com.zlt.vo.RecruiteMenuInfoVo;
import com.zlt.vo.RecruiteMenuVo;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class RecruiteMenuServiceImpl implements RecruiteMenuService {

    private RecruiteMenuDao recruiteMenuDao=new RecruiteMenuDaoImpl();

    @Override
    public RecruiteMenuVo selectRecruiteMenu() {

        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            throw new RuntimeException("当前未登录，无法查询");
        }

        //模拟数据，测试使用============

        //==========================
        List<Role> roles = activeUser.getRoles();
        if(roles==null){
            throw new RuntimeException("数据错误，用户角色查询错误");
        }

        boolean flag=false;
        for (Role role : roles) {
            if(role.getRid()==2l||role.getRid()==3l){
                flag=true;
            }
        }
        if(!flag){
            throw new RuntimeException("数据错误，用户角色查询错误");
        }

        RecruiteMenuVo recruiteMenuVo=new RecruiteMenuVo();
        RecruiteMenuInfoVo homeInfo=new RecruiteMenuInfoVo();
        homeInfo.setMname("首页");
        homeInfo.setUrl("page/welcome-1.html?t=1");
        recruiteMenuVo.setHomeInfo(homeInfo);

        RecruiteMenuInfoVo logoInfo = new RecruiteMenuInfoVo();
        logoInfo.setMname("企业客户端");
        logoInfo.setImage("images/logo.png");
        recruiteMenuVo.setLogoInfo(logoInfo);


        RecruiteMenuInfoVo menuInfo=new RecruiteMenuInfoVo();
        List<RecruiteMenuInfoVo> recruiteMenuInfoVoList=new ArrayList<>();
        recruiteMenuInfoVoList.add(menuInfo);
        recruiteMenuVo.setMenuInfo(recruiteMenuInfoVoList);

        menuInfo.setMname("常规管理");
        menuInfo.setIcon("fa fa-address-book");
        List<RecruiteMenuInfoVo> child=new ArrayList<>();
        menuInfo.setChild(child);

        //查询当前企业用户的菜单信息
        List<RecruiteMenu> recruiteMenus=new ArrayList<>();
        for (Role role : roles) {
            List<RecruiteMenu> select=recruiteMenuDao.selectMenuByUid(role.getRid());
            if(select!=null){
                recruiteMenus.addAll(select);
            }
        }

        if(recruiteMenus==null){
            throw new RuntimeException("数据错误，用户菜单查询错误");
        }


        //查询菜单一级权限
        for (RecruiteMenu recruiteMenu : recruiteMenus) {
            if(recruiteMenu.getLevels()==1){
                RecruiteMenuInfoVo recruiteMenuInfoVo=new RecruiteMenuInfoVo();
                child.add(recruiteMenuInfoVo);
                try {
                    BeanUtils.copyProperties(recruiteMenuInfoVo,recruiteMenu);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("一级权限");
        for (RecruiteMenuInfoVo recruiteMenuInfoVo : child) {
            System.out.println("一级权限===="+recruiteMenuInfoVo.toString());
        }
        System.out.println("====================");

        //封装菜单二级权限
        for (RecruiteMenu recruiteMenu : recruiteMenus) {
            for (RecruiteMenuInfoVo recruiteMenuInfoVo : child) {
                if(recruiteMenu.getParentId()==recruiteMenuInfoVo.getMid()){
                    List<RecruiteMenuInfoVo> child1=recruiteMenuInfoVo.getChild();
                    if (child1 == null){
                        child1 = new ArrayList<>();
                        recruiteMenuInfoVo.setChild(child1);
                    }
                    RecruiteMenuInfoVo recruiteMenuInfoVo1=new RecruiteMenuInfoVo();
                    child1.add(recruiteMenuInfoVo1);
                    try {
                        BeanUtils.copyProperties(recruiteMenuInfoVo1,recruiteMenu);
                        System.out.println("二级权限===="+recruiteMenuInfoVo1.toString());
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        return recruiteMenuVo;
    }
}
